/*********************************************************************************
  *FileName:  nsuc16xx_tmr.c
  *Version:   V1.0
  *Date:      2023-10-15
****************************************Includes***********************************/
#include "nsuc16xx_tmr.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/
/**
  * @brief  Initializes the TMRx Time Base Unit peripheral according to 
  *         the specified parameters in the TMR_TimeBaseInitStruct.
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  TMR_TimeBaseInitStruct: pointer to a TMR_TimeBaseInitTypeDef
  *         structure that contains the configuration information for the 
  *         specified TMR peripheral.
  * @retval None
  */
void TMR_TimeBaseInit(TIMER_TypeDef *TMRx, TMR_TimeBaseInitTypeDef *TMR_TimeBaseInitStruct)
{
    TMRx->CR_b.CKSRC = 0;
    TMRx->PSCR = TMR_TimeBaseInitStruct->TMR_Prescaler;

    if (TMR_TimeBaseInitStruct->TMR_UpdateInterrupt == ENABLE)
    {
        TMRx->CR_b.UIE = 1;
    }
    else
    {
        TMRx->CR_b.UIE = 0;
    }

    if (TMR_TimeBaseInitStruct->TMR_FinishedInterrupt == ENABLE)
    {
        TMRx->CR_b.OVIE = 1;
    }
    else
    {
        TMRx->CR_b.OVIE = 0;
    }

    if (TMR_TimeBaseInitStruct->TMR_AutoReload == ENABLE)
    {
        TMRx->CR_b.ARPE = 1;
    }
    else
    {
        TMRx->CR_b.ARPE = 0;
    }
    TMRx->PSCR = TMR_TimeBaseInitStruct->TMR_Prescaler;
    TMRx->CSVR = TMR_TimeBaseInitStruct->TMR_StartCounter;
    TMRx->CEVR = TMR_TimeBaseInitStruct->TMR_EndCounter;
}

/**
  * @brief  Initializes the TMRx Channel1 according to the specified
  *         parameters in the TMR_OCInitStruct.
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  TMR_OCInitStruct: pointer to a TMR_OCInitTypeDef structure
  *         that contains the configuration information for the specified TMR peripheral.
  * @retval None
  */

void TMR_OCInit(TIMER_TypeDef *TMRx, TMR_OCInitTypeDef *TMR_OCInitStruct)
{
    TMRx->CCCR_b.OCM = TMR_OCInitStruct->TMR_OCMode;
    if (TMR_OCInitStruct->TMR_CompareInterrupt == ENABLE)
    {
        TMRx->CCCR_b.OCIE = 1;
    }
    else
    {
        TMRx->CCCR_b.OCIE = 0;
    }

    if (TMR_OCInitStruct->TMR_ComparePreload == ENABLE)
    {
        TMRx->CCCR_b.OCPE = 1;
    }
    else
    {
        TMRx->CCCR_b.OCPE = 0;
    }

    TMRx->CCCR_b.CCS = 0; /* Timer Compare */

    TMRx->CCCR_b.CCP = TMR_OCInitStruct->TMR_OCPolarity;

    TMRx->CCCR_b.CCE = TMR_OCInitStruct->TMR_OutputState;

    TMRx->CCR = TMR_OCInitStruct->TMR_Pulse;
}

/**
  * @brief  Sets the TMRx Capture Compare1 Register value
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  Compare: specifies the Capture Compare register new value.
  * @retval None
  */
void TMR_SetCompare(TIMER_TypeDef *TMRx, uint16_t Compare)
{
    TMRx->CCR = Compare;
}

/**
  * @brief  Sets the TMRx Counter Start Register value
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  StartCounter: specifies the Counter Start Register new value.
  * @retval None
  */
void TMR_SetStartCounter(TIMER_TypeDef *TMRx, uint16_t StartCounter)
{
    TMRx->CEVR = StartCounter;
}

/**
  * @brief  Sets the TMRx Counter End Register value
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  EndCounter: specifies the Counter End Register new value.
  * @retval None
  */
void TMR_SetEndCounter(TIMER_TypeDef *TMRx, uint16_t EndCounter)
{
    TMRx->CEVR = EndCounter;
}


/**
  * @brief  Enables or disables the specified TMR peripheral.
  * @param  TIMx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  NewState: new state of the TMRx peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void TMR_Cmd(TIMER_TypeDef *TMRx, FunctionalState NewState)
{

    if (NewState != DISABLE)
    {
        /* Enable the TIM Counter */
        TMRx->CR_b.CEN = 1;
    }
    else
    {
        /* Disable the TIM Counter */
        TMRx->CR_b.CEN = 0;
    }
}

/**
  * @brief  Gets the TMRx Counter value.
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @retval Counter Register value.
  */
uint32_t TMR_GetCounter(TIMER_TypeDef *TMRx)
{
    return TMRx->CNTR;
}

/**
  * @brief  Checks whether the TMR interrupt has occurred or not.
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  TMR_IT: specifies the TMR interrupt source to check.
  *   This parameter can be one of the following values:
  *     @arg TMR_IT_Update: TMR update Interrupt source
  *     @arg TMR_IT_OC: TMR Compare Interrupt source
  *     @arg TMR_IT_IC: TMR Capture Interrupt source
  *     @arg TMR_IT_ICO: TMR Over Capture Interrupt source
  *     @arg TMR_IT_OV: TMR Couter Interrupt source 
  * @retval The new state of the TMR_IT(SET or RESET).
  */
ITStatus TMR_GetITStatus(TIMER_TypeDef *TMRx, uint8_t TMR_IT)
{
    ITStatus bitstatus = RESET;

    if ((TMRx->IPND & TMR_IT) != (uint32_t)RESET)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
  * @brief  Clears the TMRx's interrupt pending bits.
  * @param  TMRx: where x can be 0 to 1 to select the TMR peripheral.
  * @param  TMR_IT: specifies the TMR interrupt source to check.
  *   This parameter can be one of the following values:
  *     @arg TMR_IT_Update: TMR update Interrupt source
  *     @arg TMR_IT_OC: TMR Compare Interrupt source
  *     @arg TMR_IT_IC: TMR Capture Interrupt source
  *     @arg TMR_IT_ICO: TMR Over Capture Interrupt source
  *     @arg TMR_IT_OV: TMR Couter Interrupt source 
  * @retval None
  */
void TMR_ClearITPendingBit(TIMER_TypeDef *TMRx, uint8_t TMR_IT)
{
    TMRx->EIPND = TMR_IT;
}

/***************************************END OF FILE*********************************/
